using System.Collections;
using ValidationFramework.Extensions;
using ValidationFramework.Configuration.Xml;
using ValidationFramework.Configuration;
using ValidationFramework.Reflection;

namespace ValidationFramework.Specifications
{
    /// <summary>
    /// A <see cref="IRuleConfigReader"/> that creates a <see cref="EnumerableDuplicateRule"/> from a <see cref="RuleData"/>.
    /// </summary>
    /// <remarks>
    /// <see cref="RuleData"/> for default attributes.
    /// </remarks>
    /// <example>
    /// <code lang="xml" title="This example shows an xml configuration for EnumerableDuplicateRuleConfigReader">
    /// <rule 
    /// errorMessage="hello" 
    /// typeName="EnumerableDuplicateRule"
    /// equalityComparerTypeName="System.StringComparer"
    /// equalityComparerPropertyName="InvariantCulture"
    /// />
    /// </code>
    /// </example>
    /// <seealso cref="EnumerableDuplicateRule"/>
    /// <seealso cref="ConfigurationService"/>
    public sealed class ContainsNoDuplicatesParser : ISpecificationXmlParser
    {
		public ISpecification GetSpecification(System.Xml.XmlElement SpecificationElement, RuleContext context)
		{
			Guard.ArgumentNotNull(SpecificationElement, "SpecificationElement");
			if (SpecificationElement.Attributes.Count == 0)
            {
				return new ContainsNoDuplicatesSpec();
            }
            else
            {
				var equalityComparerTypeName = SpecificationElement.GetAttribute<string>("equalityComparerTypeName", "");
				var equalityComparerPropertyName = SpecificationElement.GetAttribute<string>("equalityComparerPropertyName", "");
            	var equalityComparer = (IEqualityComparer)TypeExtensions.GetStaticProperty(equalityComparerTypeName, equalityComparerPropertyName);
				return new ContainsNoDuplicatesSpec(equalityComparer);
            }
		}
	}
}